﻿using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using TotalApplication.Core;

namespace TotalApplication.AngelSix
{
    /// <summary>
    /// LoginPage.xaml 的交互逻辑
    /// </summary>
    public partial class ChatPage : BasePage<ChatMessageListViewModel>
    {
        public ChatPage()
        {
            InitializeComponent();
        }

        public ChatPage(ChatMessageListViewModel viewModel) : base(viewModel)
        {
            InitializeComponent();
        }

        #region Override Methods

        override protected void OnViewModelChanged()
        {
            // var storyboard = this.FindResource("LoadAnimation") as System.Windows.Media.Animation.Storyboard;
            if (ChatMessageList == null) return;

            var storyboard = new Storyboard();
            storyboard.AddFadeIn(1, true);
            storyboard.Begin(ChatMessageList);

            MessageText.Focus();
        }

        #endregion

        private void MessageText_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            // Get the text box
            var textbox = sender as TextBox;
            if (textbox == null)
                return;

            // Check if we have pressed enter
            if (e.Key == Key.Enter)
            {
                // If we have control pressed...
                if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control))
                {
                    // Add a new line at the point where the cursor is
                    var index = textbox.CaretIndex;

                    // Insert the new line
                    textbox.Text = textbox.Text.Insert(index, Environment.NewLine);

                    // Shift the caret forward to the newline
                    textbox.CaretIndex = index + Environment.NewLine.Length;

                    // Mark this key as handled by us
                    e.Handled = true;
                }
                else
                {
                    // Send the message
                    ViewModel.Send();

                    // Mark the key as handled
                    e.Handled = true;
                }
            }
        }
    }
}
